#include <malloc.h>
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <stdio.h>
#include "TGALoader.h"
#include "timebase.h"

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include <pspgu.h>
#include <pspgum.h>
#include <psprtc.h>
#include <string.h>
#include <pspctrl.h>
#include <psptypes.h>

#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)

//#define HIDESPLASH

struct Vertex
{
	unsigned int color;
	float x, y, z;
};

struct TexVertex
{
    float u, v;
    unsigned int color;
    float x, y, z;
};

#include "vertices.h"

void FPS(void);
void InitGU(void);
void SetupProjection(void);
void DrawScene(void);

void *dList;
void *fbp0;

int fps = 0;
char fpsDisplay[100];
u32 tickResolution;
u64 fpsTickNow;
u64 fpsTickLast;

ScePspFVector3 c_at;
ScePspFVector3 c_up;

float dt;
float xx = 1.734f;
float yy = 1.733999968f;

int run = 1;
SceUID bln;
int TimeCount;


void FPS(void)
{
	fps++;
	sceRtcGetCurrentTick( &fpsTickNow );

	if(((fpsTickNow - fpsTickLast)/((float)tickResolution)) >= 1.0f )
	{
		fpsTickLast = fpsTickNow;
		fps = 0;
	}
}

#ifdef HIDESPLASH
int step = 5;
ScePspFVector3 translate = {0, 0, 0};
#else
int step = 2;
ScePspFVector3 translate = {0, 0, 0}; //1.3f
#endif


/*
 * Step1: Splash Screens
 * Step2: Mainmenu
 * Step3: Credits
 * Step4: Highscores
 * Step5: InGame
 */

const int MONKEY_DUCK = 0;
const int MONKEY_NORM = 1;
const int MONKEY_LEFT = 2;
const int MONKEY_LEFT2 = 3;
const int MONKEY_RIGHT = 4;
const int MONKEY_RIGHT2 = 5;

typedef struct
{
    ScePspFVector3 Cam;
    ScePspFVector3 Pos;
    float JumpHeight;
    bool Jump;
    bool OnGround;
    bool InJump;
    bool Death;
    bool LockMove;
    int Bananas;
    int Area;
    int WalkSt;
    int Points;
    int Lives;
    int Level;
    int State;
    int Deathcount;
} _Monkey;

typedef struct
{
    float SplashTime;
} _Data;

_Monkey Monkey;
_Data Data;

Timebase Timing;
SceCtrlData pd;

//PROTOTYPES
bool IsRightArea(int a);
void CheckArea();
void CheckBanana();
void UpdateCoords();
void Gravity();
bool CollideTop();
void MonkeyJump();
void MonkeyNoJump();
bool CollideRight();
bool CollideLeft();
void MonkeyTurnRight();
void MonkeyTurnLeft();
bool IsDeath();
void CheckButtons();

void mcError(const char *str);
void CheckArea();
void UpdateCoords();

CTGATexture spr_misc;
CTGATexture spr_npc;
CTGATexture spr_monkey;